package com.ejlchina.tool.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.adolf.androiddemo.R
import com.adolf.androiddemo.databinding.ItemFirmwareVersionBinding
import com.ejlchina.tool.enums.UpgradeStateEnum
import com.ejlchina.tool.network.RemoteCtrlBoardVersion
import com.ejlchina.tool.singleton.GlobalParams
import com.ejlchina.tool.util.LOG_TAG
import com.ejlchina.tool.util.showToast

class FirmwareVersionAdapter(private val dataList: MutableList<RemoteCtrlBoardVersion>, private val upgradeClick: (RemoteCtrlBoardVersion) -> Unit): RecyclerView.Adapter<FirmwareVersionAdapter.FirmwareVersionViewHolder>() {
    private var itemState : UpgradeStateEnum = UpgradeStateEnum.NO_UPGRADE
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FirmwareVersionViewHolder {
        val binding = ItemFirmwareVersionBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return FirmwareVersionViewHolder(binding,upgradeClick)
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: FirmwareVersionViewHolder, position: Int) {
        holder.bind(dataList[position])
    }

    fun isItemDisable(allDisable: UpgradeStateEnum){
        itemState = allDisable
        notifyDataSetChanged()
    }

    inner class FirmwareVersionViewHolder(private val binding: ItemFirmwareVersionBinding,private val upgradeClick: (RemoteCtrlBoardVersion) -> Unit): RecyclerView.ViewHolder(binding.root){
        fun bind(remoteCtrlBoardVersion: RemoteCtrlBoardVersion){
            binding.tvVersionName.text = remoteCtrlBoardVersion.verNo
            setUpgradeBtnState(itemState)
            binding.btnUpgrade.setOnClickListener {
                if (itemState == UpgradeStateEnum.UPGRADE_DISABLE){
                    showToast("升级正在进行中")
                    return@setOnClickListener
                }
                isItemDisable(UpgradeStateEnum.UPGRADE_DISABLE)
                upgradeClick(remoteCtrlBoardVersion)
                if (GlobalParams.firmwareUpgradeList.isEmpty()){
                    showToast("未选择设备")
                    return@setOnClickListener
                }
                }
            }

        private fun setUpgradeBtnState(isInUpgrade: UpgradeStateEnum){
            when(isInUpgrade){
                UpgradeStateEnum.NO_UPGRADE -> {
                    binding.btnUpgrade.text = "升级"
                    binding.btnUpgrade.setBackgroundResource(R.drawable.ripple_co_13_so_bbe3cf)
                }
                UpgradeStateEnum.UPGRADE_ENABLE -> {
                    binding.btnUpgrade.text = "升级"
                    binding.btnUpgrade.setBackgroundResource(R.drawable.ripple_co_13_so_34c759)
                }
                UpgradeStateEnum.UPGRADE_DISABLE -> {
                    binding.btnUpgrade.text = "升级中"
                    binding.btnUpgrade.setBackgroundResource(R.drawable.ripple_co_13_so_bbe3cf)
                }
            }
        }
    }
}